﻿// -----------------------------------------------------------------------
// <copyright file="ProjectRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ProjectRepository : AbstractRepository<Project>
    {
        public ProjectRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<Project> GetAll()
        {
            var result = (from x in base._context.Projects
                          select new Project
                          {
                              Id = x.Id,
                              ProjectCode = x.ProjectCode,
                              Title = x.Title,
                              ContactPerson = x.ContactPerson,
                              ContactNumber = x.ContactNumber,
                              ProjectType = x.ProjectType,
                              Location = x.Location,
                              StartDate = x.StartDate,
                              ExpectedEndDate = x.ExpectedEndDate,
                              EndDate = x.EndDate,
                              ClientCode = x.ClientCode,
                              ContractorCode = x.ContractorCode,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Project entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Project entityToAdd = new Context.Project
            {
                Id = entity.Id,
                ProjectCode = entity.ProjectCode,
                Title = entity.Title,
                ContactPerson = entity.ContactPerson,
                ContactNumber = entity.ContactNumber,
                ProjectType = entity.ProjectType,
                Location = entity.Location,
                StartDate = entity.StartDate,
                ExpectedEndDate = entity.ExpectedEndDate,
                EndDate = entity.EndDate,
                ClientCode = entity.ClientCode,
                ContractorCode = entity.ContractorCode,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Projects.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Project entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Project entityToUpdate = base._context.Projects.Single(x => x.ProjectCode.Equals(entity.ProjectCode));

            entityToUpdate.ProjectCode = entity.ProjectCode;
            entityToUpdate.Title = entity.Title;
            entityToUpdate.ContactPerson = entity.ContactPerson;
            entityToUpdate.ContactNumber = entity.ContactNumber;
            entityToUpdate.ProjectType = entity.ProjectType;
            entityToUpdate.Location = entity.Location;
            entityToUpdate.StartDate = entity.StartDate;
            entityToUpdate.ExpectedEndDate = entity.ExpectedEndDate;
            entityToUpdate.EndDate = entity.EndDate;
            entityToUpdate.ClientCode = entity.ClientCode;
            entityToUpdate.ContractorCode = entity.ContractorCode;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Project entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Project entityToDelete = base._context.Projects.Single(x => x.ProjectCode.Equals(entity.ProjectCode));

            base._context.Projects.DeleteOnSubmit(entityToDelete);
        }
    }
}
